package fractal;

import jinyilw.common.Operator;
import jinyilw.common.ffmpeg.FFmpegTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.i18n.IString;
import jinyilw.swing.visuals.Icomp.ICheckBox;
import jinyilw.swing.visuals.Icomp.IFileChooser;
import jinyilw.swing.visuals.Icomp.ILabel;
import jinyilw.swing.visuals.Jcomp.AddFileComp;
import jinyilw.swing.visuals.Rcomp.RDialog;

import javax.swing.JDialog;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.Serial;

public class DHSetDialog extends RDialog
{
	@Serial
	private static final long serialVersionUID = -1886983271352042202L;
	private final ICheckBox enableCompressBox = new ICheckBox(
			new ICSetting("enableCompress", "启用视频压缩", "Enable video compress"));
	private final ILabel ffmpegLabel = new ILabel(new ICSetting("setFFmpeg",
			"设置FFmpeg文件路径:", "Set the FFmpeg path:"));
	private final AddFileComp addFFmpegComp = new AddFileComp();
	private final ILabel audioLabel = new ILabel(
			new ICSetting("setAudio", "选择背景音乐:", "Select background music:"));
	private final AddFileComp addAudioComp = new AddFileComp();

	public DHSetDialog(JDialog owner)
	{
		super(owner);
		setSection(12);
		disableCompress();
		enableCompressBox.addActionListener((ActionEvent e) ->
		{
			if (enableCompressBox.isSelected())
				enableCompress();
			else
				disableCompress();
		});
		addFFmpegComp.setFont(FontTools.getSystemFont(10));
		addFFmpegComp.setPreferredSize(new Dimension(320, 26));
		addFFmpegComp.addClickOperator(new Operator<>()
		{
			IFileChooser fileChooser = null;
			@Override
			public void operate(Object o)
			{
				if (fileChooser == null)
					fileChooser = new IFileChooser(new IString("setFFmpeg",
							"设置FFmpeg文件路径", "Set the FFmpeg path"));
				File file = fileChooser.openAndGetOpenFile(DHSetDialog.this);
				if (file != null)
				{
					FFmpegTools.setFFmpegFile(file);
					addFFmpegComp.setFile(file);
				}
			}
		});
		if (FFmpegTools.isExecutable())
			addFFmpegComp.setFile(FFmpegTools.getFFmpegFile());

		addAudioComp.setFont(FontTools.getSystemFont(10));
		addAudioComp.setPreferredSize(new Dimension(320, 26));
		addAudioComp.addClickOperator(new Operator<>()
		{
			IFileChooser fileChooser = null;
			@Override
			public void operate(Object o)
			{
				if (fileChooser == null)
					fileChooser = new IFileChooser(null, "mp3", new IString(
							"setAudio", "选择背景音乐", "Select background music"));
				File file = fileChooser.openAndGetOpenFile(DHSetDialog.this);
				if (file != null)
				{
					addAudioComp.setFile(file);
				}
			}
		});
		setLayout(new GridLayout(0, 1, 0, 3));
		add(enableCompressBox);
		add(ffmpegLabel);
		add(addFFmpegComp);
		add(audioLabel);
		add(addAudioComp);
		pack();
	}

	private void enableCompress()
	{
		ffmpegLabel.setEnabled(true);
		addFFmpegComp.setEnabled(true);
		audioLabel.setEnabled(true);
		addAudioComp.setEnabled(true);
	}

	private void disableCompress()
	{
		ffmpegLabel.setEnabled(false);
		addFFmpegComp.setEnabled(false);
		audioLabel.setEnabled(false);
		addAudioComp.setEnabled(false);
	}

	public boolean isEnableCompress()
	{
		return enableCompressBox.isSelected();
	}

	public File getAudioFile()
	{
		return addAudioComp.getFile();
	}
}
